package nahuel.games.carracer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;

public class Pista{
    private int velRelativaY = 0;
    private GameView gameView;
    private Bitmap bmp;
    private int currentFrame;
	private int altoD;
	private int anchoD;
    private int width;
    private int height = 0;
    private int nivel;
    Rect src = new Rect();
    Rect dst = new Rect();

	static final String FRAME = "frame";

	public Pista(int nivel, int anchoD, int altoD) {
		this.altoD = altoD;
		this.anchoD = anchoD;
		this.nivel = nivel;
		this.bmp = GameView.decodeSampledBitmap(gameView.getResources(), R.drawable.pista, this.anchoD, 0);
		this.width = bmp.getWidth();
		currentFrame = bmp.getHeight();
	}

	private void setHeight(){
		if(height == 0){
			float relacion = width * gameView.getHeight() / gameView.getWidth();
			this.height = Math.round(relacion);   //Ajusto la altura de la pista a la pantalla
			currentFrame -= height;
		}
	}
	
	public void update() {
		synchronized (gameView.getHolder()) {
			if (currentFrame <= 0) {
				currentFrame = bmp.getHeight() - height;
			}
			currentFrame -= velRelativaY;
		}
   }

    public void onDraw(Canvas canvas) {
		setHeight();
		int srcX = 0;
		int srcY = currentFrame;
		src.set(srcX, srcY, srcX + width, srcY + height);
		dst.set(0, 0, gameView.getWidth(), gameView.getHeight());
		canvas.drawBitmap(bmp, src, dst, null);
    }

    public void setVelocidadRelativa(int y){
		synchronized (gameView.getHolder()) {
			this.velRelativaY = gameView.escalarVelocidadY(y);
		}
    }

	public Bundle getBundle() {
		Bundle bund = new Bundle();
		bund.putInt(FRAME, currentFrame);
		return bund;
	}
	
	public void setBundle(Bundle bundle) {
		currentFrame = bundle.getInt(FRAME);
	}

}
